/*
 * Copyright 2020 edenrun.cn All right reserved. This software is the
 * confidential and proprietary information ofedenrun.cn ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with edenrun.cn.
 */

package com.zoom.pluto.zxg.service.impl;

import com.zoom.pluto.common.model.dto.ZxgDTO;
import com.zoom.pluto.zxg.service.ZxgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * 心血管数据访问服务实现
 *
 * @author zoom
 * @date 2020-06-27
 */
@Slf4j
@Service
public class ZxgServiceImpl implements ZxgService {
    @Override
    public List<ZxgDTO> getAllZxgInfo(String idNo) {
        List<ZxgDTO> xxggList = new ArrayList<>();
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String url = "jdbc:odbc:zxg";
            Connection con = DriverManager.getConnection(url, "", "");
            Statement sta = con.createStatement();
            ResultSet rst = sta.executeQuery("select * from XXGG where nono = \'" + idNo + "\'");

            while(rst.next()) {
                xxggList.add(getResult(rst));
            }
            return xxggList;
        } catch(Exception e) {
            log.error("获取心血管数据异常，用户身份证号：「{}」", idNo, e);
            return xxggList;
        }
    }

    @Override
    public ZxgDTO getLatestZxgInfo(String idNo) {
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String url = "jdbc:odbc:zxg";
            Connection con = DriverManager.getConnection(url, "", "");
            Statement sta = con.createStatement();
            ResultSet rst = sta.executeQuery("select * from XXGG where nono = \'" + idNo + "\' ");

            if(rst.next()) {
                return getResult(rst);
            }
            return null;
        } catch(Exception e) {
            log.error("获取心血管数据异常，用户身份证号：「{}」", idNo, e);
            return null;
        }
    }

    /**
     * 解析数据库对象
     *
     * @param rst access数据库结果
     * @return 心血管数据
     */
    private ZxgDTO getResult(ResultSet rst) {
        ZxgDTO xxgg = new ZxgDTO();
        try {
            xxgg.setNono(getString(rst, "nono"));
            xxgg.setMydate(getString(rst, "mydate"));
            xxgg.setName(getString(rst, "name"));
            xxgg.setMyMemo(getString(rst, "MyMemo"));
            xxgg.setBei(rst.getBigDecimal("bei"));
            xxgg.setT1(rst.getBigDecimal("T1"));
            xxgg.setT2(rst.getBigDecimal("T2"));
            xxgg.setTbc(rst.getBigDecimal("Tbc"));
            xxgg.setTmbb(rst.getBigDecimal("Tmbb"));
            xxgg.setTfbb(rst.getBigDecimal("Tfbb"));
            xxgg.setBc(rst.getBigDecimal("bc"));
            xxgg.setBe1(rst.getBigDecimal("be1"));
            xxgg.setBf(rst.getBigDecimal("bf"));
            xxgg.setMn(rst.getBigDecimal("mn"));
            xxgg.setDt(rst.getBigDecimal("dt"));
            xxgg.setDwmch(getString(rst, "dwmch"));
            xxgg.setXgdy(rst.getBoolean("xgdy"));
            xxgg.setNcom(rst.getInt("ncom"));
            xxgg.setPr(rst.getBigDecimal("pr"));
            xxgg.setSv(rst.getBigDecimal("sv"));
            xxgg.setCo(rst.getBigDecimal("co"));
            xxgg.setSi(rst.getBigDecimal("si"));
            xxgg.setCi(rst.getBigDecimal("ci"));
            xxgg.setVpe(rst.getBigDecimal("vpe"));
            xxgg.setEwk(rst.getBigDecimal("ewk"));
            xxgg.setHoi(rst.getBigDecimal("hoi"));
            xxgg.setHov(rst.getBigDecimal("hov"));
            xxgg.setLvwi(rst.getBigDecimal("lvwi"));
            xxgg.setCmbr(rst.getBigDecimal("cmbr"));
            xxgg.setCmbv(rst.getBigDecimal("cmbv"));
            xxgg.setCmbn(rst.getBigDecimal("cmbn"));
            xxgg.setSvr(rst.getBigDecimal("svr"));
            xxgg.setRsvr(rst.getBigDecimal("rsvr"));
            xxgg.setPp(rst.getBigDecimal("pp"));
            xxgg.setMsp(rst.getBigDecimal("msp"));
            xxgg.setMdp(rst.getBigDecimal("mdp"));
            xxgg.setMap(rst.getBigDecimal("map"));
            xxgg.setAc(rst.getBigDecimal("ac"));
            xxgg.setCcp(rst.getBigDecimal("ccp"));
            xxgg.setBlk(rst.getBigDecimal("blk"));
            xxgg.setFek(rst.getBigDecimal("fek"));
            xxgg.setTpr(rst.getBigDecimal("tpr"));
            xxgg.setSpr(rst.getBigDecimal("spr"));
            xxgg.setVer(rst.getBigDecimal("ver"));
            xxgg.setPawp(rst.getBigDecimal("pawp"));
            xxgg.setPar(rst.getBigDecimal("par"));
            xxgg.setPap(rst.getBigDecimal("pap"));
            xxgg.setBv(rst.getBigDecimal("bv"));
            xxgg.setNn(rst.getBigDecimal("nn"));
            xxgg.setNr(rst.getBigDecimal("nr"));
            xxgg.setMhr(rst.getDouble("mhr"));
            xxgg.setMrt(rst.getBigDecimal("mrt"));
            xxgg.setMst(rst.getBigDecimal("mst"));
            xxgg.setDiag(getString(rst, "diag"));
            xxgg.setIDandDate(getString(rst, "IDandDate"));
            xxgg.setDocName(getString(rst, "DocName"));
        } catch(Exception e) {
            log.error("获取信息数据库信息异常", e);
        }
        return xxgg;
    }

    /**
     * 获取结果集中的字符结果
     *
     * @param rst 结果集
     * @param key 结果Key
     * @return 字符结果
     */
    private String getString(ResultSet rst, String key) {
        try {
            byte[] resultBytes = rst.getBytes(key);
            if(null == resultBytes) {
                return "";
            }
            return new String(resultBytes, "gbk");
        } catch(Exception e) {
            log.error("获取结果集字符异常", e);
            return "";
        }
    }

}
